//
//  ViewController.m
//  19-JSON
//
//  Created by 郭宝 on 16/8/9.
//  Copyright © 2016年 郭宝. All rights reserved.
//

#import "ViewController.h"
#import "SCjCollectionViewLayout.h"
#import "TitleCollectionViewCell.h"
#import "ContentTitle.h"
@interface ViewController ()<SCjCollectionViewLayoutDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic,strong) NSMutableArray<ContentTitle *> *contentTitleArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // JSON解析
    // 通过一个Data, 将JSON格式转换成OC认识的内容
    // 数组/字典
    
    SCjCollectionViewLayout *layout = (SCjCollectionViewLayout *)self.myCollectionView.collectionViewLayout;
    layout.delegate = self;
//    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 40) collectionViewLayout:layout];
    
    [_myCollectionView registerNib:[UINib nibWithNibName:@"TitleCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"guobao"];
    // NSLog(@"%@", self.myCollection.collectionViewLayout);
    
    
    
    
    
    
   
    
    
    
    
    [self handData];
}

- (void)handData
{
    _contentTitleArray = [NSMutableArray array];
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"FirstJSON" ofType:@"json"];
     NSLog(@"%@",dataPath);
    // JSON解析
    NSError *error = nil;
    NSData *jsonData = [NSData dataWithContentsOfFile:dataPath];
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
//     NSLog(@"\n%@", jsonDic);
    
    
    NSArray *list = jsonDic[@"tabs"][@"list"];
//    NSLog(@"%@",list);
    for (NSDictionary *contentDic in list) {
        ContentTitle *contentTitle = [ContentTitle contentTitleWithDictionary:contentDic];
        [_contentTitleArray addObject:contentTitle];
        
    }
//     NSLog(@"%@",_contentTitleArray);
}








- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
                   height:(CGFloat)height
   sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ContentTitle *contentTitle = _contentTitleArray[indexPath.row];
    CGRect newRect = [contentTitle.title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, _myCollectionView.bounds.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    CGFloat width = ceilf(CGRectGetWidth(newRect));
    return width;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _contentTitleArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
//    cell.lable.text = _array[indexPath.row];
//    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1];
    
    
    cell.contentTitle = _contentTitleArray[indexPath.row];
     NSLog(@"%@",_contentTitleArray[indexPath.row]);
//    cell.backgroundColor = [UIColor greenColor];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
